package lv.rtu.ditf.dc.document;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import lv.rtu.ditf.dc.fs.Document;
import lv.rtu.ditf.dc.fs.DocumentCategory;
import lv.rtu.ditf.dc.fs.FileStorage;
import lv.rtu.ditf.dc.stemming.PorterStemmer;

/**
 * 
 * @author Pāvels Koliškins, Edgars Matisāns
 *
 */
public class DocumentVectorUtils {
    /**
	 * Return document as Vector
     * @param documentAsString String containing document data
	 * @return HashMap that represents String document as Vector, where each Vector component is
     * document word and its occurence in document
	 */
    public static HashMap<String, Integer> getDocumentVector(String documentAsString) {
        HashMap<String, Integer> documentVector = new HashMap<String, Integer>();
        
        int minWordLength = 2;
        String badPattern = "[\\s\"'().,!?/:;-]";
        String[] documentWords = documentAsString.split(badPattern);

        PorterStemmer porterStemmer = new PorterStemmer();
        for (int i = 0; i < documentWords.length; i++) {
            
            porterStemmer.add(documentWords[i].toLowerCase().toCharArray(), documentWords[i].toCharArray().length);
            porterStemmer.stem();
            String stemmedWord = porterStemmer.toString().replaceAll(badPattern, "").trim();

            if (!stemmedWord.equalsIgnoreCase("") && stemmedWord.length() >= minWordLength) {
                if (documentVector.get(stemmedWord) == null) {
                    documentVector.put(stemmedWord, 1);
                } else {
                    documentVector.put(stemmedWord, documentVector.get(stemmedWord) + 1);
                }
            }
        }
        return documentVector;
    }

    public static void main(String args[]) {
    	long start = System.currentTimeMillis();
    	
		FileStorage storage = FileStorage.get();
		try {
			List<DocumentCategory> categories = storage.getDocumentCategories();
			if (categories != null)
				for (DocumentCategory category : categories) {
					for (Document document : category.getDocuments()) {
						HashMap<String, Integer> documentVector = getDocumentVector(document.getPlainTextContent());
				        Iterator<String> it = documentVector.keySet().iterator();
				        while(it.hasNext()) {
				            String key = it.next().toString();
				            //documentVector.get(key);
				            System.out.println(key + "\t" +  documentVector.get(key));
				        }
					}
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(String.format("Time took %d ms", System.currentTimeMillis() - start));
   }
}
